package com.yjhb.android.sbz.activity;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.VipInfo;
import com.yjhb.android.sbz.mvp.vip.VipPresenter;
import com.yjhb.android.sbz.mvp.vip.VipView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.CircleImageView;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/7 17:02
 */
public class VipCenterActivity extends BaseActivity<VipPresenter> implements VipView {
    private final String TAG = "VipCenterActivity";

    @BindView(R.id.tabLayout)
    QMUITabSegment tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.user_head_iv)
    CircleImageView user_head_iv;
    @BindView(R.id.user_type_tv)
    TextView user_type_tv;
    @BindView(R.id.expired_date_tv)
    TextView expired_date_tv;

    List<VipInfo> vipInfos = new ArrayList<>();

    @Override
    protected VipPresenter initPresenter() {
        return new VipPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vip;
    }

    @Override
    protected void initUI() {
        initVipInfo();
        tabLayout.setDefaultNormalColor(getResources().getColor(R.color.gray_66));
        tabLayout.setDefaultSelectedColor(getResources().getColor(R.color.vip_color));
        viewpager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewpager);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    @Override
    public void refresh() {
        try {
            Glide.with(this).load(App.userInfo.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(user_head_iv);
            switch (App.userInfo.getVipLevel()) {
                case 100:
                    user_type_tv.setText("体验会员");
                    break;
                case 1:
                    user_type_tv.setText("月度会员");
                    break;
                case 2:
                    user_type_tv.setText("季度会员");
                    break;
                case 3:
                    user_type_tv.setText("年度会员");
                    break;
                default:
                    user_type_tv.setText("普通用户");
            }
            if (App.userInfo.getVipLevel() == 0) {
                expired_date_tv.setText("到期时间：无");
            } else {
                expired_date_tv.setText("到期时间：" + ZxTimeTool.getDate(App.userInfo.getVipEndTime().getTime(),
                        "yyyy年MM月dd日"));
            }
        } catch (Exception e) {

        }
    }

    private void initVipInfo() {
        vipInfos.clear();
        vipInfos.add(VipInfo.VIP_100);
        vipInfos.add(VipInfo.VIP_1);
        vipInfos.add(VipInfo.VIP_2);
        vipInfos.add(VipInfo.VIP_3);
    }

    PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return vipInfos.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            final VipInfo vipInfo = vipInfos.get(position);
            View v = LayoutInflater.from(getActivity()).inflate(R.layout.layout_vip, null, false);
            TextView name_tv = v.findViewById(R.id.name_tv);
            name_tv.setText(vipInfo.getName());

            TextView expireDate_tv = v.findViewById(R.id.expireDate_tv);
            if (App.userInfo.getVipLevel() == vipInfo.getLevel()) {
                if (App.userInfo.getVipLevel() == 0) {
                    expireDate_tv.setText("到期时间：无");
                } else {
                    expireDate_tv.setText("到期时间：" + ZxTimeTool.getDate(App.userInfo.getVipEndTime().getTime(),
                            "yy年MM月dd日"));
                }
            } else {
                if (vipInfo.getLevel() == 0) {
                    expireDate_tv.setText("到期时间：无");
                } else {
                    expireDate_tv.setText("未开通");
                }
            }


            TextView date_tv = v.findViewById(R.id.date_tv);
            date_tv.setText(vipInfo.getTime() == -1 ? "永久" : vipInfo.getTime() + "天");

            TextView serverCost_tv = v.findViewById(R.id.serverCost_tv);
            serverCost_tv.setText(NumberFormat.getPercentInstance(Locale.CHINA).format(vipInfo.getServerCost()));

            TextView top_tv = v.findViewById(R.id.top_tv);
            top_tv.setText(vipInfo.getTopDiscount() == 10 ? "无折扣" : vipInfo.getTopDiscount() + "折");

            TextView num3_tv = v.findViewById(R.id.num3_tv);
            TextView num4_tv = v.findViewById(R.id.num4_tv);
            TextView num5_tv = v.findViewById(R.id.num5_tv);
            TextView num6_tv = v.findViewById(R.id.num6_tv);
            TextView num7_tv = v.findViewById(R.id.num7_tv);
            TextView num8_tv = v.findViewById(R.id.num8_tv);
            View num3 = v.findViewById(R.id.num3);
            View num4 = v.findViewById(R.id.num4);
            View num6 = v.findViewById(R.id.num6);
            View num7 = v.findViewById(R.id.num7);
            View num8 = v.findViewById(R.id.num8);
            switch (vipInfo.getLevel()) {
                //体验会员
                case 100:
                    num3.setVisibility(View.VISIBLE);
                    num4.setVisibility(View.GONE);
                    num5_tv.setVisibility(View.VISIBLE);
                    num6.setVisibility(View.VISIBLE);
                    num7.setVisibility(View.VISIBLE);
                    num8.setVisibility(View.GONE);
                    num3_tv.setText("3、赠送6小时置顶（限3天内使用）");
                    num5_tv.setText("4、不限制发布个数");
                    num6_tv.setText("5、发布悬赏，优先审核");
                    num7_tv.setText("备注：体验会员每人限购一次");
                    num7_tv.setTextColor(getResources().getColor(R.color.red_00));
                    break;
                case 1:
                    num3.setVisibility(View.VISIBLE);
                    num4.setVisibility(View.GONE);
                    num5_tv.setVisibility(View.VISIBLE);
                    num6.setVisibility(View.VISIBLE);
                    num7.setVisibility(View.GONE);
                    num8.setVisibility(View.GONE);
                    num3_tv.setText("3、赠送4小时置顶（限1个月内使用）");
                    num5_tv.setText("4、不限制发布个数");
                    num6_tv.setText("5、发布悬赏，优先审核");
                    break;
                case 2:
                    num3_tv.setVisibility(View.VISIBLE);
                    num4.setVisibility(View.GONE);
                    num5_tv.setVisibility(View.VISIBLE);
                    num6.setVisibility(View.VISIBLE);
                    num7.setVisibility(View.VISIBLE);
                    num8.setVisibility(View.GONE);
                    num3_tv.setText("3、赠送10小时置顶（限2个月内使用）");
                    num5_tv.setText("4、不限制发布个数");
                    num6_tv.setText("5、发布悬赏，优先审核");
                    num7_tv.setText("6、额外赠送10天vip");
                    break;
                case 3:
                    num3.setVisibility(View.VISIBLE);
                    num4.setVisibility(View.GONE);
                    num5_tv.setVisibility(View.VISIBLE);
                    num6.setVisibility(View.VISIBLE);
                    num7.setVisibility(View.VISIBLE);
                    num8.setVisibility(View.VISIBLE);
                    num3_tv.setText("3、赠送40小时置顶（限3个月内使用）");
                    num5_tv.setText("4、不限制发布个数");
                    num6_tv.setText("5、发布悬赏，优先审核");
                    num7_tv.setText("6、额外赠送2个月vip（总共14个月）");
                    num8_tv.setText("7、不限制设备登录");
                    break;
            }

            TextView old_tv = v.findViewById(R.id.old_tv);
            old_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            old_tv.setText("原价" + vipInfo.getOldValue() + "元");

            TextView new_tv = v.findViewById(R.id.new_tv);
            new_tv.setText("限时优惠" + vipInfo.getNewValue() + "元");
            new_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new QMUIBottomSheet
                            .BottomListSheetBuilder(getActivity(), false)
                            .addItem(R.mipmap.vip_yue, "余额支付", "yue")
                            .addItem(R.mipmap.vip_pay, "支付宝/微信支付", "pay")
                            .setTitle("付款方式")
                            .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                                @Override
                                public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                    if (position == 0) {
                                        presenter.balanceToVip(vipInfo.getLevel());
                                        dialog.dismiss();
                                    } else if (position == 1) {
                                        Tool.startRechargeActivity(getActivity(), 4, "" + vipInfo.getNewValue(), true);
                                        dialog.dismiss();
                                    }
                                }
                            })
                            .build()
                            .show();


                }
            });

            if (vipInfo.getLevel() == 0) {
                old_tv.setVisibility(View.INVISIBLE);
//                new_tv.setVisibility(View.GONE);
                new_tv.setEnabled(false);
                new_tv.setText("免费");
                new_tv.setTextColor(0xFFCFCFCF);
            }

            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0)
                return "体验会员";
            return vipInfos.get(position).getName();
        }
    };

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onSuccess(Object data) {
        loadingDialog.cancel();
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
        ZxToast.showSuccessBottomToast("开通成功！");
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast(msg);
    }
}
